﻿using UnityEngine;
using System.Collections;

//单例基类
public abstract class BaseSingleton<T> : MonoBehaviour where T : Component
{
    private static T instance;

    public static T getInstance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<T>();
                if (instance = null)
                {
                    GameObject tmp = new GameObject();
                    instance = tmp.AddComponent<T>();
                }
            }
            return instance;
        }
    }

    void Awake()
    {
        if (instance == null)
        {
            instance = this as T;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            //仅能存在一个添加了T类型脚本的GameObject
            if (this != instance)
            {
                Destroy(gameObject);
            }
        }
    }
}
